/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package security;

import boundary.EventFacade;
import boundary.EventManager;
import boundary.InviteManager;
import boundary.NotificationFacade;
import boundary.NotificationManager;
import control.ForecastEngine;
import entity.Event;
import entity.Notification;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author MONESI
 */
@Named
@RequestScoped
public class LoginBean {
    @EJB
    EventFacade ef;
    
    @EJB
    NotificationFacade nf;
    
    @EJB
    NotificationManager nm;
    
    @EJB
    InviteManager im;
    
    List<String> participanti;
    
    @Inject
    private Logger logger;
    
    
    private String username;
    private String password;

    public LoginBean() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
        
    }

    public String login() {
        Date d = new Date();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            request.login(this.username, this.password);
            System.out.println("ok sono entrato");
        
        List<Event> eventi = ef.findAll();
        List<Notification> news = nf.findAll();
        
        
        ArrayList<Integer> newsId= new ArrayList<Integer>();
        
        for(int i=0;i<news.size();i++){
            newsId.add(news.get(i).getId());
        }
        
        for(int i=0;i< eventi.size();i++){
        if(!eventi.get(i).getForecast().equals("")){
            if(d.getYear()==eventi.get(i).getDate().getYear()){
                if(d.getMonth()==eventi.get(i).getDate().getMonth()){
                    if(d.getDate()+1==eventi.get(i).getDate().getDate()){
                        
                        if(eventi.get(i).getForecast().equals("Sunny") || eventi.get(i).getForecast().equals("Mostly Sunny") || eventi.get(i).getForecast().equals("Cloudy") || eventi.get(i).getForecast().equals("Rain/Snow Shower") || eventi.get(i).getForecast().equals("Partly Cloudy")){
                            
                          if(!newsId.contains(eventi.get(i).getId())){
                                
                                Notification n = new Notification();
                                n.setEventoAssociato(eventi.get(i).getId());
                                n.setContenuto("Tomorrow will be bad for your outdoor Events!");
                                n.setDestinatario(eventi.get(i).getCreator());
                                nm.save(n);
                                
                                
                                participanti = im.invitati(eventi.get(i).getId());
                                
                                System.out.println(participanti.size());
                                
                                
                                for(int j=0;j<participanti.size();j++){
                                    System.out.println(participanti.get(j));
                                Notification m = new Notification();
                                m.setEventoAssociato(eventi.get(i).getId());
                                m.setContenuto("Tomorrow will be bad for the outdoor Events in which you are invited!");
                                m.setDestinatario(participanti.get(j));
                                nm.save(m);
                                }
                            }
                        
                        
                        }
                    }
                }
            }      
        }
        }
        
        
        for(int i=0;i< eventi.size();i++){
        if(!eventi.get(i).getForecast().equals("")){
            if(d.getYear()==eventi.get(i).getDate().getYear()){
                if(d.getMonth()==eventi.get(i).getDate().getMonth()){
                    if(d.getDate()+3==eventi.get(i).getDate().getDate()){
                        //clima dell'evento in programma tre giorni dopo
                        if(eventi.get(i).getForecast().equals("Sunny") || eventi.get(i).getForecast().equals("Mostly Sunny") || eventi.get(i).getForecast().equals("Cloudy") || eventi.get(i).getForecast().equals("Rain/Snow Shower") || eventi.get(i).getForecast().equals("Partly Cloudy")){
                            
                          if(!newsId.contains(eventi.get(i).getId())){
                                
                                Notification n = new Notification();
                                n.setEventoAssociato(eventi.get(i).getId());
                                
                                ForecastEngine f = new ForecastEngine();
                                List <String> weather = f.weather(eventi.get(i));
                                
                                //clima del giorno seguente all'evento
                                System.out.println(weather.get(4));
                                if(weather.get(4).equals("Partly Cloudy")){
                                        int giornorinvio = d.getDate()+4;
                                        int meserinvio = d.getMonth()+1;
                                        n.setContenuto("In three days the events for your outdoor events will be bad. You may change the event's date with this sunny day, the closest: "+giornorinvio+"/"+meserinvio);
                                }else{
                                    n.setContenuto("In three days the events for your outdoor events will be bad.");
                                }
                                
                                n.setDestinatario(eventi.get(i).getCreator());
                                nm.save(n);
                                
                                
                                participanti = im.invitati(eventi.get(i).getId());
                                
                                System.out.println(participanti.size());
                                
                                
                                for(int j=0;j<participanti.size();j++){
                                    System.out.println(participanti.get(j));
                                Notification m = new Notification();
                                m.setEventoAssociato(eventi.get(i).getId());
                                m.setContenuto("In three days the weather will be bad for the events in which you are invited!");
                                m.setDestinatario(participanti.get(j));
                                nm.save(m);
                                }
                            }
                        
                        
                        }
                    }
                }
            }      
        }
        }
        
        
            return "ScheduleView";
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Login Failed","Login Failed"));
            logger.log(Level.SEVERE,"Login Failed");
            System.out.println("C'è un errore!");
            return null;
        }
    }
    public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        request.getSession().invalidate();
        return "index?faces-redirect=true";
    }
    
    
}